package com.gqj.demo;

import com.gg.reader.api.dal.*;
import com.gg.reader.api.protocol.gx.*;
import org.springframework.beans.factory.annotation.Value;

import java.util.Timer;
import java.util.TimerTask;


public class ReadTagsGpiTrigger extends Thread {
    private int port = -1;
    private String ipPort;

    public ReadTagsGpiTrigger(String ipPort){
        this.ipPort = ipPort;
    }

    @Override
    public void run() {
        GClient client = new GClient();
        //获取设备ip和端口号
        if (client.openTcp(ipPort, 0)) {
            port = -1;
            subscribeHandler(client);
            System.out.println("启动成功！");
        } else {
            System.out.println("启动失败！");
        }
    }

    //订阅标签信息上报与gpi触发上报
    private synchronized void subscribeHandler(GClient client) {
        System.out.println("数据接收成功！");
        client.onTagEpcLog = new HandlerTagEpcLog() {
            @Override
            public void log(String s, LogBaseEpcInfo logBaseEpcInfo) {
                if (null != logBaseEpcInfo && logBaseEpcInfo.getResult() == 0) {
                    String epc = logBaseEpcInfo.getEpc();
                    System.out.println("EPC：" + epc);
                    System.out.println("执行逻辑");
                    int i = FC.elecMaterialOutdetailService.analysisTdm(epc, port);
                }
            }

        };

        client.onTagEpcOver = new HandlerTagEpcOver() {
            public void log(String s, LogBaseEpcOver logBaseEpcOver) {
                System.out.println("HandlerTagEpcOver");
            }
        };

        client.onGpiStart = new HandlerGpiStart() {


            public void log(String s, LogAppGpiStart logAppGpiStart) {
                //索引从0开始
                if (null != logAppGpiStart) {
                    port = logAppGpiStart.getGpiPort();

                    System.out.println("端口号Level：" + logAppGpiStart.getGpiPort());
                    System.out.println("端口号：" + port);
                    System.out.println("电平" + logAppGpiStart.getGpiPortLevel());
                }

            }
        };

        client.onGpiOver = new HandlerGpiOver() {
            public void log(String s, LogAppGpiOver logAppGpiOver) {
                if (null != logAppGpiOver) {
                    //System.out.println(logAppGpiOver);
                }
            }
        };
    }


    public static void main(String[] args) {

        GClient client = new GClient();
        if (client.openTcp("192.168.1.56:8160", 0)) {

            MsgAppSetGpo msgG = new MsgAppSetGpo();
            msgG.setGpo1(1);//gpo1 设置膏电平
            client.sendSynMsg(msgG);
            if (0x00 == msgG.getRtCode()) {
                System.out.println("Set success");
            } else {
                System.out.println(msgG.getRtMsg());
            }

            System.out.println("Press any key to continue setting the GPO");

            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    msgG.setGpo1(0);//gpo1 设置低电平
                    client.sendSynMsg(msgG);
                    if (0x00 == msgG.getRtCode()) {
                        System.out.println("Set success");
                    } else {
                        System.out.println(msgG.getRtMsg());
                    }
                }
            }, 5000);
        }
    }
}
